package com.qianda.qdminiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qianda.qdminiapp.common.constant.CommonConstant;
import com.qianda.qdminiapp.common.exception.ExceptionCode;
import com.qianda.qdminiapp.common.exception.SimpleException;
import com.qianda.qdminiapp.dao.account.QqdAccountDao;
import com.qianda.qdminiapp.domain.model.account.QqdAccount;
import com.qianda.qdminiapp.domain.model.user.QqdUser;
import com.qianda.qdminiapp.dao.user.QqdUserDao;
import com.qianda.qdminiapp.security.PasswordUtils;
import com.qianda.qdminiapp.service.QqdUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cw
 * @since 2019-08-14
 */
@Service
@Transactional
public class QqdUserServiceImpl extends ServiceImpl<QqdUserDao, QqdUser> implements QqdUserService {

    @Autowired
    private QqdUserDao qqdUserDao;

    @Autowired
    private QqdAccountDao qqdAccountDao;

    @Override
    @Transactional
    public void bindPhone(String phone,String openId) {

        QueryWrapper<QqdUser> qqdUserQueryWrapper = new QueryWrapper<>();
        qqdUserQueryWrapper.eq("user_open_id",openId).eq("status",CommonConstant.Status.STATUS_NORMAL);
        QqdUser qqdUser = qqdUserDao.selectOne(qqdUserQueryWrapper);
        if(qqdUser == null){
            throw new SimpleException(ExceptionCode.EX_HTTP_INPUT_NOT_ENTATY);
        }
       // QqdUser qqdUser = qqdUserDao.selectById(userId);
        //用户绑定手机号
        qqdUser.setPhone(phone);
        qqdUser.setUpdateTime(LocalDateTime.now());

        QueryWrapper<QqdAccount> qqdAccountQueryWrapper = new QueryWrapper<>();
        qqdAccountQueryWrapper.eq("user_id",qqdUser.getId()).eq("status",CommonConstant.Status.STATUS_NORMAL);
        QqdAccount qqdAccount1 = qqdAccountDao.selectOne(qqdAccountQueryWrapper);
        if(qqdAccount1 != null){
            throw new SimpleException(ExceptionCode.EX_BIND_PHONE_EXIT);
        }

        //创建账户
        QqdAccount qqdAccount = new QqdAccount();
        qqdAccount.setUserId(qqdUser.getId());
        qqdAccount.setPhone(phone);
        qqdAccount.setPayPassword(PasswordUtils.encode("123456"));
        qqdAccount.setBalance( BigDecimal.ZERO);
        qqdAccount.setStatus(CommonConstant.Status.STATUS_NORMAL);
        qqdAccount.setCreateTime(LocalDateTime.now());
        qqdAccountDao.insert(qqdAccount);

        //账号id回填到用户表
        qqdUser.setAccountId(qqdAccount.getId());
        qqdUserDao.updateById(qqdUser);

    }

    @Override
    public Boolean checkHavePayPassWord(Long userId) {
        QqdUser qqdUser = qqdUserDao.selectById(userId);
        if(qqdUser.getAccountId() != null){
            QqdAccount qqdAccount = qqdAccountDao.selectById(qqdUser.getAccountId());
            if(!StringUtils.isEmpty(qqdAccount.getPayPassword())){
                return true;
            }
        }
        return false;
    }

    @Override
    public void setPayPassword(Long userId, String password) {
        QqdUser qqdUser = qqdUserDao.selectById(userId);
        if(qqdUser.getAccountId() != null){
            QqdAccount qqdAccount = qqdAccountDao.selectById(qqdUser.getAccountId());
            qqdAccount.setPayPassword(PasswordUtils.encode(password));
        }
    }

    @Override
    @Transactional
    public void saveUserInfo(String nickName, String avatarUrl,Long userId) {
        QqdUser qqdUser = qqdUserDao.selectById(userId);
        qqdUser.setNickname(nickName);
        qqdUser.setAvatar(avatarUrl);
        qqdUserDao.updateById(qqdUser);
    }
}
